﻿using Domain.Model;
using System.Linq;
using System.Web.Mvc;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using WebApp.Common;
using Domain.IRepositories;
using Domain.Model.ValueObject;
using Infrastructure;
using Microsoft.Office.Interop.Excel;
using System.Text;
using System.Text.RegularExpressions;
using Common;
using System.Data;
using System.Net;

namespace MVC.Controllers
{
    public class HomeController : BaseController
    {

        private IUserRepository userRpy;

        public HomeController(IUserRepository userRpy)
        {
            this.userRpy = userRpy;

        }

        // GET: Home
        public ActionResult Index()
        {

            return View();
        }


        #region 合同报告
        /// <summary>
        /// 合同报告
        /// </summary>
        /// <returns></returns>
        public ActionResult ZXBGListView()
        {
            return View();
        }

        public string GetZXBGList(MiniUIParams p)
        {

            Expression<Func<Domain.Model.User, bool>> where = Where.True<Domain.Model.User>();

            if (!string.IsNullOrEmpty(p.Key))
            {
                where = where.And(a => a.姓名.Contains(p.Key) || a.合同编号.Contains(p.Key));
            }



            try
            {
                var list = userRpy.ToPage(where, a => a, p.ToString(), p.PageIndex, p.PageSize);
                return list.ToMiniUiJson("yyyy-MM-dd hh:mm:ss");
            }
            catch (Exception e)
            {
                log.Error("征信报告获取数据异常", e);
                throw new Exception("获取数据异常");
            }

        }
        #endregion

        #region 导入
        public ActionResult ImportDataView()
        {
            return View();
        }

        public ActionResult ImportData(string p)
        {
            string fullPath = "";
            try
            {
                fullPath = Server.MapPath(p);
                if (!Directory.Exists(fullPath))
                    Directory.CreateDirectory(fullPath);
            }
            catch (Exception e)
            {
                log.Error("路径异常", e);
                or.msg = e.Message;
                or.state = 0;
            }

            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(fullPath);
            System.IO.FileSystemInfo[] files = di.GetFileSystemInfos("*", SearchOption.AllDirectories);
            // List<Domain.Model.User> list = new List<Domain.Model.User>();
            DateTime now = DateTime.Now;

            foreach (var item in files)
            {
                try
                {
                    if (string.IsNullOrEmpty(item.Extension))
                        continue;


                    string str = System.IO.File.ReadAllText(item.FullName, Encoding.Default);
                    Match match1 = Regex.Match(str, @"合同编[码|号]：(?<hetong>.*?)&nbsp;申请日期：(?<riqi>.*?)&nbsp;", RegexOptions.IgnoreCase);

                    if (match1.Success)
                    {

                        User u = new User();
                        u.合同编号 = match1.Groups["hetong"].Value.Trim();//合同编号
                        u.申请日期 = match1.Groups["riqi"].Value.Trim();//申请日期



                        int tempCount = userRpy.FindAll(a => a.合同编号 == u.合同编号, a => a, null).Count();
                        if (tempCount != 0)
                        {
                            continue;
                        }

                        Match match2 = Regex.Match(str, @"\.姓名：(?<xm>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match2.Success)
                        {
                            u.姓名 = match2.Groups["xm"].Value.Trim();
                        }

                        Match match3 = Regex.Match(str, @"\.身份证[号|号码]：(?<sfz>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match3.Success)
                        {
                            u.身份证号 = match3.Groups["sfz"].Value.Trim();
                        }

                        Match match4 = Regex.Match(str, @"\.手机：<br>(?<sj>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match4.Success)
                        {
                            u.手机 = match4.Groups["sj"].Value.Trim();
                        }

                        Match match5 = Regex.Match(str, @".现居住地址：(?<dz1>.*?)&nbsp;.*?22.镇/乡：(?<dz2>.*?)&nbsp;.*?23.街道/村：(?<dz3>.*?)&nbsp;.*?24.小区/楼盘：(?<dz4>.*?)&nbsp;.*?>25.栋/单元/房间号：(?<dz5>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match5.Success)
                        {
                            u.现居住地址 = match5.Groups["dz1"].Value + match5.Groups["dz2"].Value + match5.Groups["dz3"].Value + match5.Groups["dz4"].Value + match5.Groups["dz5"].Value;
                        }

                        Match match6 = Regex.Match(str, @"\.贷款本金（元）：(?<bj>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match6.Success)
                        {
                            u.贷款本金 = match6.Groups["bj"].Value.Trim();
                        }

                        Match match7 = Regex.Match(str, @"\.单位/学校/个体全称：(?<dw>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match7.Success)
                        {
                            u.单位_学校_个体全称 = match7.Groups["dw"].Value.Trim();
                        }

                        Match match71 = Regex.Match(str, @"67.(单位地址：省/直辖市：|单位地址：)(?<dz1>.*?)&nbsp;.*?68.镇/乡：(?<dz2>.*?)&nbsp;.*?69.街道/村：(?<dz3>.*?)&nbsp;.*?70.小区/楼盘：(?<dz4>.*?)&nbsp;.*?>71.栋/单元/房间号：(?<dz5>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match71.Success)
                        {
                            u.单位地址 = match71.Groups["dz1"].Value + match71.Groups["dz2"].Value + match71.Groups["dz3"].Value + match71.Groups["dz4"].Value + match71.Groups["dz5"].Value;
                        }
                        else
                        {

                            Match match711 = Regex.Match(str, @"66.(单位地址：省/直辖市：|单位地址：)(?<dz1>.*?)&nbsp;.*?67.镇/乡：(?<dz2>.*?)&nbsp;.*?68.街道/村：(?<dz3>.*?)&nbsp;.*?69.小区/楼盘：(?<dz4>.*?)&nbsp;.*?>70.栋/单元/房间号：(?<dz5>.*?)</td>", RegexOptions.IgnoreCase);
                            if (match711.Success)
                            {

                                u.单位地址 = match711.Groups["dz1"].Value + match711.Groups["dz2"].Value + match711.Groups["dz3"].Value + match711.Groups["dz4"].Value + match711.Groups["dz5"].Value;


                            }
                            else
                            {
                                Match match7111 = Regex.Match(str, @"68.(单位地址：省/直辖市：|单位地址：)(?<dz1>.*?)&nbsp;.*?69.镇/乡：(?<dz2>.*?)&nbsp;.*?70.街道/村：(?<dz3>.*?)&nbsp;.*?71.小区/楼盘：(?<dz4>.*?)&nbsp;.*?>72.栋/单元/房间号：(?<dz5>.*?)</td>", RegexOptions.IgnoreCase);

                                if (match7111.Success)
                                {

                                    u.单位地址 = match7111.Groups["dz1"].Value + match7111.Groups["dz2"].Value + match7111.Groups["dz3"].Value + match7111.Groups["dz4"].Value + match7111.Groups["dz5"].Value;
                                }
                            }

                        }

                        Match match8 = Regex.Match(str, @"\.单位电话：(?<dwdh>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match8.Success)
                        {
                            u.单位电话 = match8.Groups["dwdh"].Value.Trim();
                        }

                        Match match9 = Regex.Match(str, @"销售顾问姓名:&nbsp;(?<gw>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match9.Success)
                        {
                            u.销售顾问姓名 = match9.Groups["gw"].Value.Trim();
                        }



                        Match match10 = Regex.Match(str, @"销售点名称：(?<gw>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match10.Success)
                        {
                            u.销售点名称 = match10.Groups["gw"].Value.Trim();
                        }

                        Match match11 = Regex.Match(str, @"销售点地址：(?<gw>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match11.Success)
                        {
                            u.销售点地址 = match11.Groups["gw"].Value.Trim();
                        }


                        Match match12 = Regex.Match(str, @"配偶姓名：(?<gw>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match12.Success)
                        {
                            u.配偶姓名 = match12.Groups["gw"].Value.Trim().Replace("&nbsp;", "");
                        }


                        Match match13 = Regex.Match(str, @"配偶移动电话：(?<gw>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match13.Success)
                        {
                            u.配偶移动电话 = match13.Groups["gw"].Value.Trim().Replace("&nbsp;", "");
                        }

                        Match match14 = Regex.Match(str, @"家庭成员名称：(?<gw>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match14.Success)
                        {
                            u.家庭成员名称 = match14.Groups["gw"].Value.Trim();
                        }

                        Match match15 = Regex.Match(str, @"家庭成员类型：(?<gw>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match15.Success)
                        {
                            u.家庭成员类型 = match15.Groups["gw"].Value.Trim();
                        }

                        Match match16 = Regex.Match(str, @"家庭成员电话：(?<gw>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match16.Success)
                        {
                            u.家庭成员电话 = match16.Groups["gw"].Value.Trim();
                        }

                        Match match17 = Regex.Match(str, @"联系人姓名：(?<gw>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match17.Success)
                        {
                            u.联系人姓名 = match17.Groups["gw"].Value.Trim();
                        }

                        Match match18 = Regex.Match(str, @"联系电话：(?<gw>.*?)</td>", RegexOptions.IgnoreCase);
                        if (match18.Success)
                        {
                            u.联系电话 = match18.Groups["gw"].Value.Trim();
                        }
                        Match match19 = Regex.Match(str, @"客户银行卡号/账号：<br>&nbsp;&nbsp;&nbsp;&nbsp;(?<yhkh>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match19.Success)
                        {
                            u.客户银行卡号 = match19.Groups["yhkh"].Value.Trim();
                        }

                        Match match20 = Regex.Match(str, @"客户银行卡号/账号：<br>&nbsp;&nbsp;&nbsp;&nbsp;(?<yhkh>.*?)&nbsp;</td>", RegexOptions.IgnoreCase);
                        if (match20.Success)
                        {
                            u.客户开户银行 = match20.Groups["yhkh"].Value.Trim();
                        }

                        Match match21 = Regex.Match(str, @"客户开户银行：<br>&nbsp;&nbsp;&nbsp;&nbsp;(?<khkhyh>.*?)&nbsp;</td>", RegexOptions.IgnoreCase);
                        if (match21.Success)
                        {
                            u.客户银行账户名 = match21.Groups["khkhyh"].Value.Trim();
                        }


                        Match match22 = Regex.Match(str, @"户籍地址：(?<hjdz1>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match22.Success)
                        {
                            u.户籍地址 = match22.Groups["hjdz1"].Value.Trim();

                            match22 = Regex.Match(str, @"17.镇/乡：(?<hjdz1>.*?)&nbsp;", RegexOptions.IgnoreCase);

                            if (match22.Success)
                            {
                                u.户籍地址 += match22.Groups["hjdz1"].Value.Trim();
                            }

                            match22 = Regex.Match(str, @"18.街道/村：(?<hjdz1>.*?)&nbsp;", RegexOptions.IgnoreCase);

                            if (match22.Success)
                            {
                                u.户籍地址 += match22.Groups["hjdz1"].Value.Trim();
                            }
                            match22 = Regex.Match(str, @"19.小区/楼盘：(?<hjdz1>.*?)&nbsp;", RegexOptions.IgnoreCase);

                            if (match22.Success)
                            {
                                u.户籍地址 += match22.Groups["hjdz1"].Value.Trim();
                            }

                            match22 = Regex.Match(str, @"20.栋/单元/房间号：(?<hjdz1>.*?)&nbsp;", RegexOptions.IgnoreCase);

                            if (match22.Success)
                            {
                                u.户籍地址 += match22.Groups["hjdz1"].Value.Trim();
                            }

                        }

                        Match match23 = Regex.Match(str, @"品牌\(1\)：(?<cp>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match23.Success)
                        {
                            u.产品= match23.Groups["cp"].Value.Trim();

                            match23 = Regex.Match(str, @"商品型号\(1\)：(?<cp>.*?)&nbsp;", RegexOptions.IgnoreCase);
                            if (match23.Success)
                            {
                                u.产品 += match23.Groups["cp"].Value.Trim();
                            }
                        }

                        Match match24= Regex.Match(str, @"分期期数：(?<fqs>.*?)&nbsp;", RegexOptions.IgnoreCase);
                        if (match24.Success)
                        {
                            u.分期数= match24.Groups["fqs"].Value.Trim();
                        }

                        u.ID = Guid.NewGuid();
                        u.CreateTime = now;
                        u.ReMark = item.Name;
                        userRpy.Add(u);
                        userRpy.Commit();
                        //移到成功文件

                        FileHelper.removeTo(item.FullName, item.FullName.Replace("OData", "SData"));


                    }



                }
                catch (Exception e)
                {
                    log.Error("添加单个异常", e);
                    or.msg += e.Message + "\n";
                    or.state = 0;
                }
            }
            or.msg += "转完";
            return Json(or);

        }
        #endregion


        #region 导出EXLE
        public void OutExcel()
        {
            userRpy.OutExcle();

        }
        #endregion


        public void DeleteData()
        {
            this.userRpy.Remove(a => true);

        }

        public ActionResult LoginView()
        {
            return View();
        }

        public ActionResult Login(string str)
        {
            //string loginUrl = "http://218.17.36.84:7001/bqjr/logon.html";
            //CookieContainer cc = new CookieContainer();
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginUrl);
            //request.Method = "GET";
            //request.CookieContainer = cc;



            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //cc.Add(response.Cookies);
            //Stream stream = response.GetResponseStream();
            //ViewBag.WebContent = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
            HttpHelp hh = new HttpHelp("http://218.17.36.84:7001/bqjr/Logon.jsp", str);

            CookieContainer cc = hh.Get_Login();
            string jsonUrl = "http://218.17.36.82:8090/bqxs/windCtrlDailyController.do?storesData&PosCode=";
            string jsonData = str;

            //1、首先获取数据条数
            string jsonStr = hh.GetJsonStr(jsonUrl, jsonData);
            return Content("ok");
        }
    }
}